﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using bibliotecaObservador;
using Npgsql;
using System.Data;

namespace SIGEB.Modelo
{
    public abstract class conjuntoBase:Observable
    {
        #region "constructores"

        public conjuntoBase(datosAplicacion dA)
        {
            datos = dA;
            setFormatoFecha();
        }

        #endregion

        #region "métodos"

        protected string[] datosDataSet(DataRow ds, int n)
        {
            string[] nuevo = new string[n];
            for (int i = 0; i < n; i++)
            {
                nuevo[i] = Convert.ToString(ds[i]);
            }
            return nuevo;
        }

        private void setFormatoFecha()
        {
            try
            {
                compConexion.Open();
                string sql = "SHOW DATESTYLE";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                ds.Reset();
                da.Fill(ds);
                dt = ds.Tables[0];
                string[] valores = null;
                foreach (DataRow row in dt.Rows)
                {
                    valores = datosDataSet(row, 1);
                    formatoFecha = (valores[0].Split(',')[1].ToUpper()).Trim();
                }
            }
            catch
            {
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch { }
            }
        }

        public string formatDate(DateTime fecha)
        {
            string[] a = new string[3];
            a[pos(formatoFecha, 'D')] = fecha.Day.ToString();
            a[pos(formatoFecha, 'M')] = fecha.Month.ToString();
            a[pos(formatoFecha, 'Y')] = fecha.Year.ToString();
            return a[0] + '-' + a[1] + '-' + a[2];
        }

        private static int pos(string cadena, char a)
        {
            int count = 0;
            foreach (char actual in cadena)
            {
                if (actual == a)
                {
                    return count;
                }
                count++;
            }
            return -1;
        }

        public DateTime formatDate(string fecha)
        {
            string[] campos = fecha.Split('-', '/', ' ');
            int[] dias = new int[3];
            dias[2] = int.Parse(campos[pos(formatoFecha, 'Y')]);
            dias[1] = int.Parse(campos[pos(formatoFecha, 'M')]);
            dias[0] = int.Parse(campos[pos(formatoFecha, 'D')]);
            DateTime nuevo = new DateTime(dias[2], dias[1], dias[0]);
            return nuevo;
        }

        #endregion

        #region "atributos"

        private int _numTipo;

        protected datosAplicacion datos;

        string formatoFecha;

        public int numTipo
        {
            protected set { _numTipo = value; }
            get { return _numTipo; }
        }

        protected NpgsqlConnection compConexion
        {
            get { return datos.conexion; }
        }

        protected DataSet ds = new DataSet();

        protected DataTable dt = new DataTable();

        protected Hashtable tabla;

        public static int CONJDOCUMENTOS = 1;
        public static int CONJTIPODOCS = 2;
        public static int CONJEJEMPLARES = 3;
        public static int CONJGRUPOS = 4;
        public static int CONJAUTORES = 5;
        public static int CONJNOTIFICACIONES = 6;
        public static int CONJPERFILES = 7;
        public static int CONJTIPOSPERFIL = 8;
        public static int CONJPRESTAMOS = 9;
        public static int CONJUSUARIOS = 10;
        public static int CONJTIPOUSUARIO = 11;
        public static int CONJATRIBUTOS = 12;
        public static int CONJINFOCOMPUTADORAS = 13;
        public static int CONJVALORES = 14;
        public static int CONJRESERVAS = 15;
        public static int CONJEDITORIALES = 16;
        public static int CONJMULTA = 17;
        
        #endregion 
    }
}
